﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.Profinet.Siemens;

namespace 项目框架
{
    public partial class FrmPlc : Form
    {
        private SiemensS7Net siemensTcpNet = null;
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        public FrmPlc()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            timer.Tick += timer_tick;
            timer.Interval = 100;
        }
        private void timer_tick(object sender, EventArgs e)
        {
            if (read.IsSuccess && read != null)
            {
                if (siemensTcpNet != null)
                {
                    Form1.data.Status = true;
                    var te = siemensTcpNet.ByteTransform.TransBool(read.Content, 0, 1);
                    input.start = te[0];
                    input.stop = te[1];
                    input.valve1_open = te[2];
                    input.actualValue = siemensTcpNet.ByteTransform.TransSingle(read.Content, 2);
                    input.level = siemensTcpNet.ByteTransform.TransInt16(read.Content, 8);
                }
                else
                {
                    timer.Stop();
                    Form1.data.Status = false;
                    input.start = false;
                    input.valve1_open = false;
                }
            }

        }
        private void ucBtn1_Click(object sender, EventArgs e)
        {
            this.BeginInvoke(new Action(() =>
            {
                ucBtn1.Enabled = false;
            }));
            siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1500, textBox1.Text);
            siemensTcpNet.ConnectTimeOut = 100;
            if (siemensTcpNet.ConnectServer().IsSuccess)
            {
                timer.Start();
                this.BeginInvoke(new Action(() =>
                {
                    richTextBox1.Text = "连接成功";
                    Form1.UpdataStatus(true);
                }));
                Thread th = new Thread(new ThreadStart(cycle));
                th.Start();
            }
            else
            {
                this.BeginInvoke(new Action(() =>
                {
                    Form1.UpdataStatus(false);
                    ucBtn1.Enabled = true;
                    richTextBox1.Text = "连接失败";
                }));
                siemensTcpNet.Dispose();
                siemensTcpNet = null;
            }
        }
        private void ucBtn2_Click(object sender, EventArgs e)
        {
            try
            {
                ucBtn1.Enabled = true;
                if (siemensTcpNet != null)
                {
                    OperateResult disconnect = siemensTcpNet.ConnectClose();
                    siemensTcpNet.Dispose();
                    siemensTcpNet = null;
                    richTextBox1.Text = disconnect.IsSuccess ? "断开连接" : "断开失败";
                    Form1.UpdataStatus(false);
                }
                else
                {
                    Form1.UpdataStatus(false);
                    MessageBox.Show("还没有连接");
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                MessageBox.Show("还没有连接");
            }
        }
        OperateResult<byte[]> read = new OperateResult<byte[]>();
        void cycle()
        {
            while (siemensTcpNet != null)
            {
                try
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Restart();
                    read = siemensTcpNet.Read("DB2.0", 493);//读取DB2.0开始地址的10个字节
                    sw.Stop();
                    this.BeginInvoke(new Action(() =>
                    {
                        richTextBox1.Text = sw.Elapsed.TotalMilliseconds.ToString() + "ms";
                    }));
                }
                catch
                {
                    break;
                }
                Thread.Sleep(100);
            }
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            ucBtn2_Click(sender, e);
            siemensTcpNet = null;
        }
    }
}
